﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using GeoFramework;
using System.Drawing;
using MapControl.NativeApi;

namespace MapControl
{
    public class Marker : MapElement
    {
        private TransparentImage image;
        public TransparentImage Image
        {
            get
            {
                return image;
            }
            set
            {
                image = value;
                if(image != null)
                    Size = image.ImageBitmap.Size;
            }
        }

        public Marker(Position position)
            : this(position, null)
        {

        }

        public Marker(Position position, TransparentImage image)
        {
            Position = position;
            Image = (image != null) ? image : MarkersIcons.DefaultMarker;
        }

        protected override Rectangle CalculateClickableArea()
        {
            int x = VisibleAreaOnMap.X - (VisibleAreaOnMap.Width / 2);
            if (x < 0)
                x = 0;
            int y = VisibleAreaOnMap.Y - VisibleAreaOnMap.Height;
            if (y < 0)
                y = 0;
            return new Rectangle(x, y, VisibleAreaOnMap.Width, VisibleAreaOnMap.Height);
        }

        protected override void Paint(System.Drawing.Graphics g)
        {
            //using (Bitmap backBuffer = new Bitmap(Size.Width, this.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            //{
                //using (Graphics gxBuffer = Graphics.FromImage(backBuffer))
                //{
                    int x = VisibleAreaOnMap.X - (VisibleAreaOnMap.Width / 2);
                    if (x < 0)
                        x = 0;
                    int y = VisibleAreaOnMap.Y - VisibleAreaOnMap.Height;
                    if (y < 0)
                        y = 0;
                    //gxBuffer.Clear(Map.BackColor);
                    IntPtr hdcDest = g.GetHdc();
                    RECT dstRect = new RECT(x, y, Size.Width, Size.Height);
                    // Ask the Image object from Imaging to draw itself.
                    image.Image.Draw(hdcDest, ref dstRect, IntPtr.Zero);
                    g.ReleaseHdc(hdcDest);
                    // Put the final composed image on screen.
                    //g.DrawImage(backBuffer, x, y);
                //}
            //}
        }

        protected override void DisposeElement()
        {
            if (image != null)
                image.Dispose();
        }
    }
}
